/**
 * @author flashflexpro@gmail.com
 * Date: 11-1-28
 * Time: ����4:48
 */
package com.flashflexpro.flexdirector.console.code {
import com.flashflexpro.flexdirector.constant.DirectorMetadata;
import com.flashflexpro.flexdirector.model.DtoAppInstReplay;
import com.flashflexpro.flexdirector.model.DtoTestCase;

import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;

public class GenerateMavenPoms {

    private var _testCase:DtoTestCase;

    public function GenerateMavenPoms(testCase:DtoTestCase) {
        _testCase = testCase;
    }

    public function execute():void {
        var modules:String;

        var previousPath:String = null;
        for (var tmpObj:Object in _testCase.appInst2code) {
            var appInst:DtoAppInstReplay = tmpObj as DtoAppInstReplay;
            if( previousPath == null ){
                previousPath = appInst.srcOutPutPath;
                modules = "        <module>@module@</module>\n".replace( /@module@/g, appInst.getId() );
            }
            else if( previousPath == appInst.srcOutPutPath ){
                modules += "        <module>@module@</module>\n".replace( /@module@/g, appInst.getId() );
            }
            else if( previousPath != appInst.srcOutPutPath ){
                writeLeadPom( previousPath, modules );
                previousPath = appInst.srcOutPutPath;
            }

            saveAppInstSrc(appInst);
        }
        writeLeadPom( previousPath, modules );
    }
    private function writeLeadPom( path:String, modules:String ):void{

        var rt:String = MAVEN_POM_LEAD.replace(/@testCaseId@/g, _testCase.getId() );

        rt = rt.replace( /@modules@/g, modules );


        var fs:FileStream = new FileStream();
        var tf:File = new File( path + File.separator + "pom.xml" );
        fs.open( tf, FileMode.WRITE);
        fs.writeUTFBytes( rt );
        fs.close();
    }

    private function saveAppInstSrc(dtoTargetAppInst:DtoAppInstReplay):void {
        var code:String = MAVEN_POM_INST.replace(/@appInstId@/g, dtoTargetAppInst.getId() );
        code = code.replace(/@metadata@/g, DirectorMetadata.metaName);

        var fs:FileStream = new FileStream();
        var tf:File = new File( dtoTargetAppInst.srcOutPutPath + File.separator + dtoTargetAppInst.getId() + File.separator + "pom.xml" );
        fs.open( tf, FileMode.WRITE);
        fs.writeUTFBytes( code );
        fs.close();
    }


    public static const MAVEN_POM_INST:String = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<project xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" +
            "         xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
            "         xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
            "    <modelVersion>4.0.0</modelVersion>\n" +
            "\n" +
            "    <groupId>com.flashflexpro</groupId>\n" +
            "    <artifactId>flexdirectorgenerated-@appInstId@</artifactId>\n" +
            "    <version>1.0-SNAPSHOT</version>\n" +
            "    <packaging>swc</packaging>\n" +
            "\n" +
            "    <name>flexdirectorgenerated-@appInstId@</name>\n" +
            "    <properties>\n" +
            "        <flexmojos.version>3.8</flexmojos.version>\n" +
            "        <flex.sdk.version>3.5.0.12683</flex.sdk.version>\n" +
            "    </properties>\n" +
            "    <build>\n" +
            "        <finalName>flexdirectorgenerated-@appInstId@</finalName>\n" +
            "        <sourceDirectory>src</sourceDirectory>\n" +
            "        <plugins>\n" +
            "            <plugin>\n" +
            "                <groupId>org.sonatype.flexmojos</groupId>\n" +
            "                <artifactId>flexmojos-maven-plugin</artifactId>\n" +
            "                <version>${flexmojos.version}</version>\n" +
            "                <extensions>true</extensions>\n" +
            "                <configuration>\n" +
            "                    <storepass/>\n" +
            "                    <keepAs3Metadatas>\n" +
            "                        <name>@metadata@</name>\n" +
            "                    </keepAs3Metadatas>\n" +
            "                    <debug>true</debug>\n" +
            "                    <targetPlayer>9.0.124</targetPlayer>\n" +
            "                    <configurationReport>true</configurationReport>\n" +
            "                </configuration>\n" +
            "                <dependencies>\n" +
            "                    <dependency>\n" +
            "                        <groupId>com.adobe.flex</groupId>\n" +
            "                        <artifactId>compiler</artifactId>\n" +
            "                        <version>${flex.sdk.version}</version>\n" +
            "                        <type>pom</type>\n" +
            "                    </dependency>\n" +
            "                </dependencies>\n" +
            "            </plugin>\n" +
            "        </plugins>\n" +
            "    </build>\n" +
            "    <dependencies>\n" +
            "        <dependency>\n" +
            "            <groupId>com.adobe.flex.framework</groupId>\n" +
            "            <artifactId>flex-framework</artifactId>\n" +
            "            <version>${flex.sdk.version}</version>\n" +
            "            <type>pom</type>\n" +
            "            <scope>external</scope>\n" +
            "        </dependency>\n" +
            "        <dependency>\n" +
            "            <groupId>com.adobe.flexunit</groupId>\n" +
            "            <artifactId>flexunit</artifactId>\n" +
            "            <version>4.0-rc-1</version>\n" +
            "            <type>swc</type>\n" +
            "        </dependency>\n" +
            "    </dependencies>\n" +
            "</project>";

    public static const MAVEN_POM_LEAD:String =
        "<?xml version=\"1.0\"?>\n" +
                "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                "         xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n" +
                "    <modelVersion>4.0.0</modelVersion>\n" +
                "\n" +
                "    <groupId>com.flashflexpro</groupId>\n" +
                "    <artifactId>flexdirectorgenerated@testCaseId@</artifactId>\n" +
                "    <version>1.0-SNAPSHOT</version>\n" +
                "    <packaging>pom</packaging>\n" +
                "\n" +
                "    <name>flexdirectorgenerated@testCaseId@</name>\n" +
                "\n" +
                "    <modules>\n" +
                "@modules@" +
                "    </modules>\n" +
                "\n" +
                "</project>";
}
}
